//
//  MYGFindTableCell.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/4/18.
//

import UIKit
import QMUIKit

class MYGFindTableCell: UITableViewCell {

    // MARK: - Property
    var changeAction: (()->Void)?
    var likeType = 0
    var model: MYGFindListModel? {
        didSet {
            guard let model = model else {
                return
            }
            avatar.sd_setImage(with: URL(string: model.doctor.avatar),placeholderImage: UIImage(named: "head"))
            nameLabel.text = model.doctor.name
            timeLabel.text = model.updated_at
            contentLabel.text = model.content
            playBtn.isHidden = true
            if model.type == 2 {
                playBtn.isHidden = false
                if model.medias.count > 0 {
                    let name = model.medias.first!
                    bkImageView.videoImageWithvideoURL(URL(string:name)!, atTime: 1000) {[weak self] image in
                        guard let image = image, let self = self else {
                            return
                        }
                        //self.bkImageView.image = image
                    }
                }else {
                    playBtn.isHidden = true
                    bkImageView.image = UIImage(named: "Chat/Function/image")
                }
                
            } else {
                playBtn.isHidden = true
                if model.medias.count > 0 {
                    let name = model.medias.first!
                    bkImageView.sd_setImage(with: URL(string: "\(name)"), placeholderImage: UIImage(named: "Chat/Function/image"))
                }else {
                    bkImageView.image = UIImage(named: "Chat/Function/image")
                }
                
            }

            starButton.setTitle("\(model.likes)", for: .normal)
            starButton.isSelected = model.is_like > 0
            
            messageButton.setTitle("\(model.comments)", for: .normal)
            shareButton.setTitle("\(model.shares)", for: .normal)
            if model.doctor_id == MYGUserManager.getUser()?.doctor.id {
                followButton.isHidden = true
            }else {
                followButton.isHidden = false
                followButton.isSelected = model.is_followup == 1

            }
        }
    }
    
    
    // MARK: - init
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        contentView.backgroundColor = .white
        
        contentView.addSubview(avatar)
        contentView.addSubview(nameLabel)
        contentView.addSubview(timeLabel)
        contentView.addSubview(followButton)
        contentView.addSubview(contentLabel)
        contentView.addSubview(bkImageView)
        contentView.addSubview(playBtn)
        
        avatar.snp.makeConstraints { make in
            make.left.equalTo(27.5)
            make.top.equalTo(19.5)
            make.width.height.equalTo(52)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(avatar.snp.right).offset(7)
            make.top.equalTo(avatar)
            make.height.equalTo(22.5)
        }
        
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(5)
        }
        
        followButton.snp.makeConstraints { make in
            make.centerY.equalTo(avatar)
            make.height.equalTo(25)
            make.width.equalTo(56)
            make.right.equalToSuperview().offset(-17.5)
        }
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(avatar)
            make.right.equalToSuperview().offset(-17.5)
            make.top.equalTo(avatar.snp.bottom).offset(12.5)
            make.height.equalTo(42)
        }
        
        bkImageView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(contentLabel.snp.bottom)
            make.height.equalTo(200)
        }
        
        playBtn.snp.makeConstraints { make in
            make.edges.equalTo(bkImageView)
        }
        
        contentView.addSubview(starButton)
        contentView.addSubview(messageButton)
        contentView.addSubview(shareButton)
       
        starButton.snp.makeConstraints { make in
            make.left.equalTo(16.5)
            make.top.equalTo(bkImageView.snp.bottom).offset(13.5)
            make.height.equalTo(20)
            make.width.equalTo(50)
        }
        
        messageButton.snp.makeConstraints { make in
            make.centerY.width.height.equalTo(starButton)
            make.left.equalTo(starButton.snp.right).offset(9.5)
        }
        
        shareButton.snp.makeConstraints { make in
            make.centerY.width.height.equalTo(starButton)
            make.left.equalTo(messageButton.snp.right).offset(9.5)
        }
        
        contentView.addSubview(line)
        line.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        
    }
    

    // MARK: - lazy load
    private lazy var avatar: UIImageView = {
        let imgV = UIImageView()
        imgV.sd_setImage(with: URL(string: "https://img0.baidu.com/it/u=440666296,1667611490&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1709917200&t=8c3514d50769bb0e0e0fdf4867fc1df1"))
        imgV.layer.masksToBounds = true
        imgV.layer.cornerRadius = 20
        return imgV
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(13)
        label.textColor = .rgba(102, 102, 102, 1)
        return label
    }()
    
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(14)
        label.textColor = .rgba(51, 51, 51, 1)
        label.text = "2分钟前"
        return label
    }()
    
    private lazy var followButton: QMUIButton = {
        let btn = QMUIButton()
        btn.setTitleColor(.rgba(255, 76, 108, 1), for: .normal)
        btn.titleLabel?.font = .regular(13)
        btn.setTitle("+关注", for: .normal)
        btn.setTitle("已关注", for: .selected)
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 12.5
        btn.layer.borderColor = UIColor.rgba(255, 76, 108, 1).cgColor
        btn.layer.borderWidth = 0.5
        
        btn.addTarget(self, action: #selector(clickButton(_:)), for: .touchUpInside)
        return btn
    }()
    
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(14)
        label.textColor = .rgba(51, 51, 51, 1)
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var bkImageView:UIImageView = {
        let bkView = UIImageView()
        bkView.layer.cornerRadius = 4
        bkView.layer.masksToBounds = true
        return bkView
    }()
    
    
    private var playBtn:UIButton = {
        let playBtn = UIButton()
        playBtn.setImage(UIImage(named: "MYG_home_play"), for: .normal)
        playBtn.isUserInteractionEnabled = false
        return playBtn
    }()
    
    private lazy var starButton: QMUIButton = {
        let btn = QMUIButton()
        btn.setImage(UIImage(named: "Find/heart"), for: .normal)
        btn.setImage(UIImage(named: "great_sel"), for: .selected)
        btn.spacingBetweenImageAndTitle = 6
        btn.imagePosition = .left
        btn.setTitle("685", for: .normal)
        btn.setTitleColor(.rgba(72, 68, 67, 1), for: .normal)
        btn.titleLabel?.font = .regular(10)
        return btn
    }()
    
    private lazy var messageButton: QMUIButton = {
        let btn = QMUIButton()
        btn.setImage(UIImage(named: "Find/message"), for: .normal)
        btn.setImage(UIImage(named: "Find/greate_sel"), for: .selected)
        btn.spacingBetweenImageAndTitle = 6
        btn.imagePosition = .left
        btn.setTitle("685", for: .normal)
        btn.setTitleColor(.rgba(72, 68, 67, 1), for: .normal)
        btn.titleLabel?.font = .regular(10)
        return btn
    }()
    
    private lazy var shareButton: QMUIButton = {
        let btn = QMUIButton()
        btn.setImage(UIImage(named: "Find/share"), for: .normal)
        btn.setImage(UIImage(named: "Find/greate_sel"), for: .selected)
        btn.spacingBetweenImageAndTitle = 6
        btn.imagePosition = .left
        btn.setTitle("685", for: .normal)
        btn.setTitleColor(.rgba(72, 68, 67, 1), for: .normal)
        btn.titleLabel?.font = .regular(10)
        return btn
    }()
    private lazy var line:UIView = {
        let line = UIView()
        line.backgroundColor = .rgba(108, 123, 138, 0.08)
        return line
    }()
    
}

extension MYGFindTableCell {
    
    @objc func clickButton (_ sender:QMUIButton){
        sender.isSelected = !sender.isSelected
        
        if sender.isSelected == true {
            MYGLoading.show()
            MMYGNetwork<MYGCommonAPI,MYGEmptyModel>.request(target: .followAction(id: "\(self.model!.doctor_id)", prefix: "followUpDoctor")) { _ in
                MYGLoading.hide()
                self.model?.is_followup = 1
                self.changeAction!()
            }  failed: { _, error in
                MYGLoading.hide()
                MMYGHUD.showToast(text: error)
            }
            
        }else {
            MYGLoading.show()
            MMYGNetwork<MYGCommonAPI,MYGEmptyModel>.request(target: .unfollowAction(id: "\(self.model!.doctor_id)", prefix: "followUpDoctor")) { _ in
                MYGLoading.hide()
                self.model?.is_followup = 0
                self.changeAction!()
            }  failed: { _, error in
                MYGLoading.hide()
                MMYGHUD.showToast(text: error)
            }
        }

        
    }
    @objc func onLike(_ sender: UIButton) {
        var likeStatus = "1"
        if self.model!.is_like == 0 {
            likeStatus = "1"
            self.model!.likes = max(0, (self.model!.likes + 1))
            self.model!.is_like = 1

        }else {
            likeStatus = "0"
            self.model!.likes = max(0, (self.model!.likes - 1))
            self.model!.is_like = 0
        }
        MYGNetwork<MYGMeAPI, MYGEmptyModel>.request(target: .likeSubmit(param: [
            "id":"\(self.model?.id)",
           "like_type":"\(likeType)",
            "like_status":likeStatus])) { [self] item in
            starButton.isSelected = true
       } failed: { _, error in
           MMYGHUD.showToast(text: error)
       }
    }
    
    
}
